package com.wswy.wzcx.ui.weather

import android.graphics.*
import android.graphics.drawable.Drawable
import android.os.Build
import com.wswy.wzcx.widget.drawable.BaseDrawable
import android.graphics.DashPathEffect
import android.support.v4.content.ContextCompat
import com.wswy.wzcx.AppContext
import com.wswy.wzcx.R


class ProxyDrawable (private val topBg:Drawable?=null): BaseDrawable() {

    private val mRectF = RectF()

    private var topLeftRadius: Float = 30f

    private val mXorMode = PorterDuffXfermode(PorterDuff.Mode.XOR)

    private val path = Path()

    private val dashPath =  DashPathEffect(floatArrayOf(6f, 24f), 0f)

    private val dashColor = ContextCompat.getColor(AppContext.getContext(), R.color.textColorPrimary_20)

    override fun draw(c: Canvas) {

        c.let {canvas ->

            mRectF.set(rect)

            val saveCount =  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                canvas.saveLayer(mRectF,null)
            }else{
                canvas.saveLayer(mRectF, null, Canvas.ALL_SAVE_FLAG)
            }


            canvas.drawARGB(0,0,0,0)

            paint.xfermode = null

            paint.color = Color.WHITE
            canvas.drawPaint(paint)


            val offset = mRectF.height()* (800f/1659f)

            topBg?.setBounds(0,0,rect.width(),offset .toInt())
            topBg?.draw(canvas)

            //drawTop(canvas)
            drawCentLine(canvas)

            paint.xfermode = mXorMode
            paint.color = Color.WHITE


            val offset2 = mRectF.height()* (0.7f)
            canvas.drawCircle(0f, offset2, topLeftRadius, paint)
            canvas.drawCircle(mRectF.right, offset2, topLeftRadius, paint)

            drawBottom(canvas)

            canvas.restoreToCount(saveCount)

        }

    }

    private fun drawTop(canvas: Canvas){
        val w = mRectF.width() - topLeftRadius - topLeftRadius

        //1.5dp 6dp
        val r = 4.5f
        val s = 18f
        val t = ((w ) / (r*2 + s)).toInt()

        val offset =  (w-((w / (r*2 + s)) * (r*2 + s) - s ))/2

        val dy = mRectF.height()* (2f/3f)

        for (i in 0..t){
            val dx = topLeftRadius + r * 2 + offset + (i * (r*2 + s))
            if (dx + r *2 < (w+topLeftRadius)){
                canvas.drawCircle( dx,dy , r, paint)
            }

        }
    }

    private fun drawCentLine(canvas: Canvas){
        val offset = mRectF.height()* (0.7f)
        val w = mRectF.width() - topLeftRadius  - 10
        paint.color = dashColor

        path.reset()
        path.moveTo(topLeftRadius+10,offset)
        path.lineTo(w,offset)

        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 6f
        paint.strokeCap = Paint.Cap.ROUND
        paint.pathEffect = dashPath

        //canvas.drawLine(topLeftRadius+5,offset,w,offset,paint)

        canvas.drawPath(path, paint)

        paint.style = Paint.Style.FILL
        paint.pathEffect = null
    }

    private fun drawBottom(canvas: Canvas){

        paint.style = Paint.Style.FILL

        val w = mRectF.width()
        val r = 18f
        val t = ((w - r *2 ) / (r*3)).toInt()


        val offset =  (w-((w / (r*3)) * (r*3) - r ))/2
        val dy = mRectF.height()

        for (i in 0..t){
            val dx = r *2 + offset + (i * r * 3)
            if (dx + r *2 < w){
                canvas.drawCircle( dx,dy , r, paint)
            }

        }
    }

}